# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

from PricingEngine_BlackScholesAnalyticalMod import *
from CalibratorMod import *

def runTest():
    mp = ModelParameters()
    mp.p['type'] = 'call'
    bsme = BlackScholesAnalytical(mp)
    op = OptimiseParameters()
    op.p['vol'] = 0.25
    pp = PricingParameters(['strike', 'tenor', 'rate', 'initial', 'div'])
    pp.addPricingSet([100, 30, 0.05, 100, 0],0)
    bsme.setUpParameters(pp,op)
    #print(bsme.getPrice())

    targetPrice = 86.0
    pp = PricingParameters(['strike', 'tenor', 'rate', 'initial', 'div'])
    pp.addPricingSet([100, 30, 0.05, 100, 0], targetPrice)
    dm = SumOfSquaresDistance()
    c = ScipyFminBFGSCalibrator(bsme, pp, op, dm)
    res = c.calibrate()
    return res['vol']
    
if __name__ == "__main__":
    res = runTest()
    print(res)